export const BASE_URL = 'https://chongwu.goldsu.cn/pet-api'
// export const BASE_URL = 'http://192.168.31.53:12082/'
import {
	loginRequest
} from './login.js';
import store from '../store'
let past=false
let time=false
export const myRequest = (url, data = {}, method) => {
	store.commit('switch', {
		code: true
	})
	var token = uni.getStorageSync("token") || "";
	return new Promise((resolve, reject) => {
		let urlScreen = ['/api/user/home/v1/cage', '/api/user/mine/v1/petCate/options',
			'/api/driver/order/v1/page', '/api/system/v1/basic/set',
			'/api/driver/home/v1/authCheck', '/api/driver/home/v1/carBrand',
			'/api/driver/mine/v1/update/location','/api/system/v1/sms/get'
		]
		console.log('url--', url, uni.getStorageSync("authStatus") !== 3 && urlScreen.indexOf(url) === -1)
		let authStatus = uni.getStorageSync("authStatus")
		if ((authStatus == 1 || authStatus == 2) && urlScreen.indexOf(url) === -1) return decide()

		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			data: data || {},
			header: {
				"XX-Device-Type": "wxapp",
				// 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' ,   
				"Content-Type": "application/json",
				"XX-User-Type": 'user',
				token,
			},
			success: (res) => {
				if (res.data.code == '200') {
					resolve(res.data)
				} else if (res.data.code == '20010' || res.data.code == '20009' || res.data
					.code == '20000') {
					if(past) return;
					past=true
					uni.showModal({
						title: '提示',
						content: '登录已过期，请刷新',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								past=false
								loginRequest()
							}
						}
					});
					// uni.removeStorageSync('userInfo');
					// uni.removeStorageSync('token');
					// setTimeout(() => {
					// 	uni.showToast({
					// 		title: '您还未登录，请先登录',
					// 		icon: "none"
					// 	})
					// }, 1000) 
					// uni.reLaunch({
					// 	url: "/pages/login/login"
					// })
				} else if (res.data.code == '20011') {
					uni.$u.throttle(() => {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									uni.removeStorageSync('token');
									loginRequest()
								}
							}
						});
					}, 10000)
				} else if (res.data.code == '30088') {
					uni.showModal({
						title: '提示',
						content: res.data.msg,
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}
						}
					});
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
				}


			},
			fail: (err) => {
				uni.showToast({
					title: '请求失败，请检查网络！',
					icon: "none"
				})
				reject(err.data)
			},
			complete() {
				store.commit('switch', {
					code: false
				})
			}
		})
	}).catch((e) => {})
}

function decide(){
	if(time) return;
	
	time = true
	setTimeout(()=>{
		time=false
	},2000)
	uni.showModal({
		title: '提示',
		content: '当前用户未认证通过，请先认证！',
		showCancel: false,
		success: function(res) {
			if (res.confirm) {
				uni.redirectTo({
					url: '/pages/joinDriver/joinDriver'
				})
			}
		}
	});
}